<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<%@ include file="/commons/basejs.jsp" %>
<script type="text/javascript">

function saveArticle() {
	var catalogIds = $("#cmsCatalog").combotree("getValues");
	$("#catalogIds").val(catalogIds);
    $('#fmArticle').form('submit', {
        url: '${path}/admin/article/save',
        onSubmit: function() {
         progressLoad();
                var isValid = $(this).form('validate');
                if (!isValid) {
                    progressClose();
                }
                return isValid;
        },
        success: function(result) {
         	progressClose();
            result = $.parseJSON(result);
            if (result.success) {
                 parent.$.messager.show({title: '提示！',msg:result.msg});
             }else {
                 parent.$.messager.show({title: '提示！',msg:result.msg});
            }
        }
    });
}
function openImageWin(){
	 parent.$.modalDialog({
            title : '相册管理',
            width: 780,
		    height: 430,
		    maximizable:true,
		    top: ($(window).height() - 520) * 0.5,
		    left: ($(window).width() - 580) * 0.5,
            href : '${path }/admin/album/openImageWin',
            buttons : [ {
                text : '确定',
                handler : function() {
	                var ids = [];
					var imgUrl = '';
                    var f = parent.$.modalDialog.handler.find('#DataGrid');
                    var rows = f.datagrid('getSelections');
					if (rows.length==0){ 
						parent.$.messager.alert({title: '提示',msg: '请选择信息！'});
					    return ;
					}
					for(var i=0;i<rows.length;i++){
						ids.push(rows[i].id);
						imgUrl=rows[i].url;
					}
					$("#img0").attr("src","${path}/"+imgUrl);
					$("#thumbnail").val("${path}/"+imgUrl);
                }
            } ]
        });
}	
</script>
<div class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',border:false" title="" style="padding: 3px;">
	<form id="fmArticle" action="save" method="post" >
		<input type="hidden" name="id" id="id" value="${article.id}"/>
		<input type="hidden" name="catalogIds" id="catalogIds" value="${catalogIds}"/>
			<table class="grid">
				<tr>
					<td style="width: 100px;" align="center">标题:</td>
					<td colspan="6">
						<input type="text" name="title" class="easyui-textbox" required="true"  data-options="validType:'length[1,128]'" style="width:800px;" value="${article.title}"/>
					</td>
					<td rowspan="5" colspan="2" >
						<%-- <img alt="缩略图" src="${staticPath }/static/style/images/nothumbnail.jpg" id="img0" style="width: 196px;height:196px; "> --%>
						<img alt="缩略图" <c:choose> <c:when test="${empty article.thumbnail}"> src="${staticPath }/static/style/images/nothumbnail.jpg"  </c:when><c:otherwise>src="${article.thumbnail}"</c:otherwise></c:choose> id="img0" style="width: 196px;height:196px; ">
						<input type="hidden" name="thumbnail" id="thumbnail" value="${article.thumbnail}"/>
					</td>
				</tr>
				<tr>
					 <td align="center" >类别:</td>
					<td>
						 <select class="easyui-combobox" name="catalogCode" required="true" style="width:200px;">
						    <c:forEach var="dList" items="${dictList}">
						   	 	<option value="${dList.dictcode}">${dList.dictname}</option>
							</c:forEach>
					    </select>
					</td>
					 <td align="center">作者:</td>
					<td>
						 <input style="width: 200px;" type="text" name="author" class="easyui-textbox"  maxlength="200" value="${article.author}"/>
					</td>
					 <td align="center">序号:</td>
					<td>
						 <input style="width: 200px;" type="text" name="sequence" class="easyui-numberbox" min="0" max="9999999"
					required="true" maxlength="200" value="${article.sequence}"/>
					</td>
					
				</tr>
					
				<tr>
					 <td align="center">头条新闻:</td>
				<td>
					<input type="radio" name="isheadlines" id="isheadlines1" value="1" <c:choose> <c:when test="${article.headlineNews== '1'}"> checked="checked" </c:when></c:choose>/>
					<label for="status1">
						是
					</label>
					<input type="radio" name="isheadlines" id="isheadlines0" value="0" <c:choose> <c:when test="${article.headlineNews== '0'}"> checked="checked" </c:when></c:choose>  />
					<label for="status0">
						否
					</label>
				</td>
				
				 <td align="center">焦点新闻:</td>
				<td>
					<input type="radio" name="isFocus" id="isfocus1" value="1" <c:choose> <c:when test="${article.isFocus== '1'}"> checked="checked" </c:when></c:choose>/>
					<label for="status1">
						是
					</label>
					<input type="radio" name="isFocus" id="isfocus0" value="0" <c:choose> <c:when test="${article.isFocus== '0'}"> checked="checked" </c:when></c:choose>  />
					<label for="status0">
						否
					</label>
				</td>
				 <td align="center">有效标志:</td>
					<td>
						<input type="radio" name="status" id="status1" value="1" <c:choose> <c:when test="${article.status=='1'}"> checked="checked" </c:when></c:choose>  />
						<label for="status1">
							有效
						</label>
						<input type="radio" name="status" id="status0" value="0"  <c:choose> <c:when test="${article.status=='0'}"> checked="checked" </c:when></c:choose>/>
						<label for="status0">
							无效
						</label>
					</td>
				</tr>
				<tr>
					 <td align="center">摘要:</td>
					<td colspan="5">
					    <input class="easyui-textbox" name="summary" data-options="multiline:true" style="width:100%;height:45px" value="${article.summary}"/>
					</td>
					
				</tr>
				<tr>
					 <td align="center">重定向地址:</td>
					<td  colspan="3">
						<input  style="width:450px;" type="text" name="redirectUrl" class="easyui-textbox" maxlength="200" value="${article.redirectUrl}"/>
						<span class="word_surplus"></span>
					</td>
					 <td align="center">来源:</td>
					<td>
						<input type="text" style="width:200px;" name="source" class="easyui-textbox" maxlength="200" value="${article.source}"/>
					</td>
				</tr>
				<tr>
					<td rowspan="3" colspan="7">
						<div >
						    <textarea id="editor" type="text" name="content" style="width:100%;height: 400px; ">${article.content}</textarea>
						</div>
					</td>
					<td colspan="2">
						<input type="button" onclick="openImageWin()" id="uploadImg" value="选择缩略图" class="easyui-linkbutton" iconCls="icon-upload">
					</td>
				</tr>
				<tr>
					<td colspan="2">请选择栏目：
					    <select name="cmsCatalog" id="cmsCatalog" class="easyui-combotree" style="width:200px;"
       						 data-options="url:'${path }/catalog/tree',required:true,multiple:true,editable: false,checkbox: true">
    					</select>
					</td>
				</tr>
				<tr></tr>
			</table>
	</form>
	</div>
	<div region="south"
		style="height: 40px; border-top: 1px solid #eee; background: #fafafa; padding: 5px 15px;text-align: right;" align="right">
		<div style="margin-top: 0px; margin-bottom: 0px;">
		  
			<a href="#" class="easyui-linkbutton" id="save" iconCls="icon-save"
				onclick="saveArticle()">保存</a>
		   
			<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" id="cancel"
				onclick="parent.$('#index_tabs').tabs('close','文章编辑');">取消</a>
		 			
		</div>
	</div>
</div>

<script type="text/javascript" charset="utf-8" src="${staticPath }/static/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="${staticPath }/static/ueditor/ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="${staticPath }/static/ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript">

//建立一個可存取到該file的url
function getObjectURL(file) {
	var url = null ; 
	if (window.createObjectURL!=undefined) { // basic
		url = window.createObjectURL(file) ;
	} else if (window.URL!=undefined) { // mozilla(firefox)
		url = window.URL.createObjectURL(file) ;
	} else if (window.webkitURL!=undefined) { // webkit or chrome
		url = window.webkitURL.createObjectURL(file) ;
	}
	return url ;
}
    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
  $(function(){
  
  $("#cmsCatalog").combotree({//加载一个combotree,并展开所有节点，因为展开后才能显示选中的值  
        onLoadSuccess:function(node,data){  
        var _cids = new Array();
    	var cids='${catalogIds}';
    	_cids=cids.split(',');
    	$('#cmsCatalog').combotree('setValues', _cids);
    	var t = $("#cmsCatalog").combotree('tree');
    	t.tree('expandAll',node.target);
        }  
    });  

  
	$("#file0").change(function(){
	var objUrl = getObjectURL(this.files[0]) ;
	console.log("objUrl = "+objUrl) ;
	if (objUrl) {
		$("#img0").attr("src", objUrl) ;
	}
}) ;
	//富文本编辑器
	UE.getEditor('editor');
	
	UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
	UE.Editor.prototype.getActionUrl = function(action){
		if(action == '/ueditor/upload/images'){
		alert(action);
			return '${path}'+'/ueditor/upload/images';
		}else{
			return this._bkGetActionUrl.call(this, action);
		}
	}
});


</script>
